#include<iostream>

using namespace std;
const int N=30010;

struct node
{
	int f;
	int w,cnt;
}ufs[N];

int find(int x)
{
	if(ufs[x].f==x) return x;
	int f=ufs[x].f;
	ufs[x].f=find(f);
	ufs[x].w=ufs[x].w+ufs[f].w;
	return ufs[x].f;
}

void un(int x,int y)
{
	int fx=find(x);
	int fy=find(y);

	if(fx==fy) return;

	ufs[fx].f=fy;
	ufs[fx].w=ufs[fy].cnt;
	ufs[fy].cnt+=ufs[fx].cnt;
}

int quary(int x,int y)
{
	int fx=find(x);
	int fy=find(y);

	if(fx!=fy) return -1;

	return abs(-ufs[x].w+ufs[y].w)-1;
}

int main()
{
	int t;cin>>t;
	for(int i=1;i<=N;i++)
	{
		ufs[i].f=i;
		ufs[i].w=0;
		ufs[i].cnt=1;
	}
	for(int i=1;i<=t;i++)
	{
		char a;cin>>a;
		int x,y;cin>>x>>y;

		if(a=='M')
		{
			un(x,y);
		}
		else
		{
			cout<<quary(x,y)<<endl;
		}
	}
}